//
//  CYABStatisticsVC.m
//  CreditAddressBook
//
//  Created by Lee on 15/10/10.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYABStatisticsVC.h"
#import "CYStatistics.h"
#import "CYDialTool.h"
#import "CYStatisticsTopView.h"
#import "CYDatabase.h"

@interface CYABStatisticsVC ()

@property(strong, nonatomic) CYStatistics *stat;//统计模型
@property(strong, nonatomic) CYStatisticsTopView *topView;//通讯录
@end

@implementation CYABStatisticsVC

- (CYStatistics *)stat {
    if (!_stat) {
        _stat = [[CYStatistics alloc] init];
    }
    return _stat;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - 数据库操作
/**
 *  汇总数据
 */
- (void)loadCollectData:(NSInteger)index {
    NSDictionary *totalparams = nil;
    NSDictionary *callOutParams = nil;
    NSDictionary *firstCallParams = nil;
    switch (index) {
        case CYIndexSecond:
            totalparams = @{@"queryDurationAndTimes": @YES, @"time": [CYDialTool timeWithStyle:CYTimeStyleDay]};
            callOutParams = @{@"queryComeOutDurationAndTimes": @YES};
            firstCallParams = @{@"timeAsc": @YES};
            break;
            
        case CYIndexThird:
            totalparams = @{@"queryDurationAndTimes": @YES, @"time": [CYDialTool timeWithStyle:CYTimeStyleWeek]};
            callOutParams = @{@"queryComeOutDurationAndTimes": @YES};
            firstCallParams = @{@"timeAsc": @YES};
            break;
            
        case CYIndexFourth:
            totalparams = @{@"queryDurationAndTimes": @YES, @"time": [CYDialTool timeWithStyle:CYTimeStyleMonth]};
            callOutParams = @{@"queryComeOutDurationAndTimes": @YES};
            firstCallParams = @{@"timeAsc": @YES};
            break;
            
        default:
            totalparams = @{@"queryDurationAndTimes": @YES};
            callOutParams = @{@"queryComeOutDurationAndTimes": @YES};
            firstCallParams = @{@"timeAsc": @YES};
            break;
    }
    NSArray *totalarr = [[CYDatabase sharedDatabase] callHistorysWithParam:totalparams];
    NSArray *callOutarr = [[CYDatabase sharedDatabase] callHistorysWithParam:callOutParams];
    self.stat.callRecord = totalarr.lastObject;
    CYCallRecord *callRecord = callOutarr.lastObject;
    self.stat.callOutDuration = callRecord.callDurationSum;
    self.stat.callOutTimes = callRecord.callHistory.num;
    CYCallRecord *firstCall = [[CYDatabase sharedDatabase] callHistorysWithParam:firstCallParams].firstObject;
    NSTimeInterval time = 0;
    if (firstCall.callHistory.createTime) {
        time = [NSDate date].timeIntervalSince1970 - firstCall.callHistory.createTime;
        self.stat.averageCallTimes = (time/self.stat.callRecord.callHistory.num)/3600;
        self.stat.averageCallDuration = [CYDialTool callTime:self.stat.callRecord.callHistory.durationSum/callRecord.callHistory.num];
    }
}
@end
